#ifndef HELPERS_H
#define HELPERS_H

#include <ctime>
#include <cassert>
#include <sstream>
#include <boost/foreach.hpp>

const time_t c_nullDate = static_cast<time_t>(0);

static time_t makeTime(int year, int month, int day)
{
  assert(1970 <= year && year <= 2037); // UNIX Epoch
  assert(1 <= month && month <= 12);
  assert(1 <= day && day <= 31);

  tm timeInfo;
  memset(&timeInfo, 0, sizeof(timeInfo));
  timeInfo.tm_year = year - 1900;
  timeInfo.tm_mon = month - 1; // From 0 to 11 :) Idiots.
  timeInfo.tm_mday = day; // From 1 to 31 :) Idiots.
  return mktime(&timeInfo);
}

template <typename T>
T as(const std::string& value)
{
  std::stringstream stream;
  stream.str(value);
  T result;
  stream >> result;
  return result;
}

template <typename T>
std::string asString(const T& value)
{
  std::ostringstream stream;
  stream << value;
  return stream.str();
}

static time_t dateFromFormattedString(const std::string& value)
{
  std::istringstream stream;
  stream.str(value);

  int year = 0;
  int month = 0;
  int day = 0;

  stream >> year >> month >> day;
  return makeTime(year, month, day);
}

#endif HELPERS_H